<template>
  <div class="login-container">
    <!-- 背景装饰 -->
    <div class="background-decoration">
      <div class="circle circle-1"></div>
      <div class="circle circle-2"></div>
      <div class="circle circle-3"></div>
    </div>

    <!-- 左侧装饰区域 -->
    <div class="left-section">
      <div class="welcome-content">
        <div class="logo-section">
          <div class="logo-icon">
            <!-- 这里需要放置公司logo图片 -->
            <img src="@/assets/logo/xmlg_logo.png" alt="Logo" class="logo-img" />
          </div>
          <h1 class="company-name">智行企业</h1>
        </div>
        <div class="welcome-text">
          <h2>欢迎登录知识之海</h2>
          <p>体验企业级组织管理平台，让工作更高效，让管理更智能。</p>
        </div>
        <div class="feature-list">
          <div class="feature-item">
            <span class="feature-icon">🚀</span>
            <span class="feature-text">高效管理</span>
          </div>
          <div class="feature-item">
            <span class="feature-icon">📊</span>
            <span class="feature-text">数据分析</span>
          </div>
          <div class="feature-item">
            <span class="feature-icon">🔒</span>
            <span class="feature-text">安全可靠</span>
          </div>
        </div>
      </div>
    </div>

    <!-- 右侧登录表单 -->
    <div class="right-section">
      <div class="login-form-container">
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
          <div class="form-header">
            <h3 class="form-title">登录账户</h3>
            <p class="form-subtitle">请输入您的登录信息</p>
          </div>

          <el-form-item prop="username">
            <div class="input-wrapper">
              <span class="input-icon">
                <svg-icon icon-class="user"/>
              </span>
              <el-input
                ref="username"
                v-model="loginForm.username"
                placeholder="请输入用户名"
                name="username"
                type="text"
                tabindex="1"
                auto-complete="on"
                class="form-input"
              />
            </div>
          </el-form-item>

          <el-form-item prop="password">
            <div class="input-wrapper">
              <span class="input-icon">
                <svg-icon icon-class="password"/>
              </span>
              <el-input
                :key="passwordType"
                ref="password"
                v-model="loginForm.password"
                :type="passwordType"
                placeholder="请输入密码"
                name="password"
                tabindex="2"
                auto-complete="on"
                class="form-input"
                @keyup.enter.native="handleLogin"
              />
              <span class="show-pwd" @click="showPwd">
                <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"/>
              </span>
            </div>
          </el-form-item>



          <el-button
            :loading="loading"
            type="primary"
            class="login-button"
            @click.native.prevent="handleLogin"
          >
            <span v-if="!loading">立即登录</span>
            <span v-else>登录中...</span>
          </el-button>

          <div class="form-footer">
            <div class="footer-links">
              <a href="#" class="footer-link">忘记密码？</a>
              <a href="#" class="footer-link">联系管理员</a>
            </div>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import {validUsername} from '@/utils/validate'
import {setToken} from '@/utils/auth'
import { login } from '@/api/user'

export default {
  name: 'Login',
  data() {
    // 用户名校验规则
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('请输入正确的用户名'))
      } else {
        callback()
      }
    }

    // 密码校验规则
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码长度至少为6位'))
      } else {
        callback()
      }
    }



    // 数据模型
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loginRules: {
        username: [{required: true, trigger: 'blur', validator: validateUsername}],
        password: [{required: true, trigger: 'blur', validator: validatePassword}]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },

  mounted() {
  },

  methods: {
    // 展示密码
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },



    // 登录方法
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          console.log(11111);

          login(this.loginForm).then((result) => {
            console.log(result);

            if (result.data.code === 1) {
              setToken(result.data.data)
              this.$message.success('登录成功')
              console.log('login success')
              this.$router.push('/')
              this.loading = false
            }else{
              this.$message.error(result.data.msg)
              this.loading = false
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* Element UI 样式重置 */
.login-container {
  .el-input {
    input {
      background: transparent;
      border: none;
      border-radius: 0;
      padding: 12px 15px 12px 45px;
      color: #333;
      height: 50px;
      font-size: 14px;

      &::placeholder {
        color: #999;
      }

      &:focus {
        outline: none;
      }
    }
  }

  .el-form-item {
    margin-bottom: 24px;

    .el-form-item__error {
      color: #f56565;
      font-size: 12px;
      margin-top: 5px;
    }
  }

  .el-button {
    border: none;
    border-radius: 8px;
  }
}
</style>

<style lang="scss" scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  position: relative;
  overflow: hidden;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

/* 背景装饰 */
.background-decoration {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 1;
}

.circle {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  animation: float 6s ease-in-out infinite;
}

.circle-1 {
  width: 200px;
  height: 200px;
  top: 10%;
  left: 10%;
  animation-delay: 0s;
}

.circle-2 {
  width: 150px;
  height: 150px;
  top: 60%;
  right: 15%;
  animation-delay: 2s;
}

.circle-3 {
  width: 100px;
  height: 100px;
  bottom: 20%;
  left: 20%;
  animation-delay: 4s;
}

@keyframes float {
  0%, 100% {
    transform: translateY(0px) rotate(0deg);
  }
  50% {
    transform: translateY(-20px) rotate(180deg);
  }
}

/* 左侧装饰区域 */
.left-section {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 60px;
  position: relative;
  z-index: 2;
}

.welcome-content {
  max-width: 500px;
  color: white;
  text-align: center;
}

.logo-section {
  margin-bottom: 40px;
}

.logo-icon {
  width: 80px;
  height: 80px;
  margin: 0 auto 20px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(10px);
}

.logo-img {
  width: 50px;
  height: 50px;
  object-fit: contain;
}

.company-name {
  font-size: 32px;
  font-weight: 700;
  margin: 0;
  background: linear-gradient(45deg, #fff, #f0f8ff);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.welcome-text {
  margin-bottom: 40px;
}

.welcome-text h2 {
  font-size: 28px;
  font-weight: 600;
  margin: 0 0 15px 0;
  opacity: 0.95;
}

.welcome-text p {
  font-size: 16px;
  line-height: 1.6;
  opacity: 0.8;
  margin: 0;
}

.feature-list {
  display: flex;
  justify-content: center;
  gap: 30px;
  flex-wrap: wrap;
}

.feature-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

.feature-icon {
  font-size: 24px;
  width: 50px;
  height: 50px;
  background: rgba(255, 255, 255, 0.15);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(10px);
}

.feature-text {
  font-size: 14px;
  opacity: 0.9;
}

/* 右侧登录表单 */
.right-section {
  width: 480px;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px);
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  z-index: 2;
}

.login-form-container {
  width: 100%;
  max-width: 400px;
  padding: 60px 40px;
}

.form-header {
  text-align: center;
  margin-bottom: 40px;
}

.form-title {
  font-size: 28px;
  font-weight: 700;
  color: #2d3748;
  margin: 0 0 8px 0;
}

.form-subtitle {
  font-size: 14px;
  color: #718096;
  margin: 0;
}

.input-wrapper {
  position: relative;
  background: #f7fafc;
  border: 2px solid #e2e8f0;
  border-radius: 12px;
  transition: all 0.3s ease;

  &:focus-within {
    border-color: #667eea;
    background: #fff;
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
  }
}

.input-icon {
  position: absolute;
  left: 15px;
  top: 50%;
  transform: translateY(-50%);
  color: #a0aec0;
  z-index: 1;
  transition: color 0.3s ease;
}

.input-wrapper:focus-within .input-icon {
  color: #667eea;
}

.form-input {
  width: 100%;
}

.show-pwd {
  position: absolute;
  right: 15px;
  top: 50%;
  transform: translateY(-50%);
  color: #a0aec0;
  cursor: pointer;
  transition: color 0.3s ease;
  z-index: 1;

  &:hover {
    color: #667eea;
  }
}



.login-button {
  width: 100%;
  height: 50px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  font-size: 16px;
  font-weight: 600;
  margin-top: 8px;
  transition: all 0.3s ease;

  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 25px rgba(102, 126, 234, 0.3);
  }

  &:active {
    transform: translateY(0);
  }
}

.form-footer {
  margin-top: 30px;
  text-align: center;
}

.footer-links {
  display: flex;
  justify-content: space-between;
  gap: 20px;
}

.footer-link {
  color: #718096;
  text-decoration: none;
  font-size: 14px;
  transition: color 0.3s ease;

  &:hover {
    color: #667eea;
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-container {
    flex-direction: column;
  }

  .left-section {
    padding: 40px 20px;
    min-height: 40vh;
  }

  .right-section {
    width: 100%;
    min-height: 60vh;
  }

  .login-form-container {
    padding: 40px 20px;
  }

  .welcome-content {
    max-width: 100%;
  }

  .company-name {
    font-size: 24px;
  }

  .welcome-text h2 {
    font-size: 22px;
  }

  .feature-list {
    gap: 20px;
  }
}
</style>
